package thread;

import java.util.Random;

public class ThreadDemo10 {
    public static int oddNum = 0;
    public static int evenNum = 0;
    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        int n = 1000_0000;
        int[] arr = new int[n];
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int num = random.nextInt(100) + 1;
            arr[i] = num;
        }
        Thread t = new Thread(() ->{
           int sum = 0;
           for(int i = 0; i < 1000_0000; i = i + 2){
               sum += arr[i];
           }
            evenNum = sum;
            System.out.println("偶数和为:"+evenNum);
        });
        Thread tt = new Thread(() ->{
            int sum = 0;
            for(int i = 1; i < 1000_0000; i = i + 2){
                sum += arr[i];
            }
            oddNum = sum;
            System.out.println("奇数和为:"+oddNum);
        });
        t.start();
        tt.start();

        t.join();
        tt.join();

        System.out.println("合为:" +(oddNum+evenNum));
        long endTime = System.currentTimeMillis();
        System.out.println("运行时间:"+(endTime-startTime));
    }
}
